package dao.impl;

import dao.ShoppingcartDao;
import model.Shoppingcart;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * The type Shoppingcart dao.
 *
 * @author wyt
 * @create 2022 -12-12 15:05
 */
public class ShoppingcartDaoImpl implements ShoppingcartDao {
    /**
     * The Pstmt.
     */
    PreparedStatement pstmt = null;
    /**
     * The Rs.
     */
    ResultSet rs = null;

    @Override
    public List<Long> find(long uid, int pageIndex, int pageSize) {
        List<Long> list = new ArrayList<>();
        String sql = "SELECT sid FROM shoppingcart WHERE uid=? LIMIT ?,?";
        Connection conn = BaseDao.getConnection();
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setLong(1, uid);
            pstmt.setInt(2, (pageIndex - 1) * pageSize);
            pstmt.setInt(3, pageSize);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                list.add(rs.getLong("sid"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(rs, pstmt, conn);
        }
        return list;
    }

    @Override
    public List<Long> findSidByUid(long uid) {
        List<Long>list=new ArrayList<>();
        String sql = "SELECT sid FROM shoppingcart WHERE uid=?";
        Connection conn = BaseDao.getConnection();
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setLong(1, uid);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                list.add(rs.getLong("sid"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(rs, pstmt, conn);
        }
        return list;
    }

    @Override
    public int insert(Shoppingcart shoppingcart) {
        int rows = 0;
        String sql = "INSERT INTO shoppingcart(scid,uid,sid) VALUES(?,?,?)";
        Connection conn = BaseDao.getConnection();
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setLong(1, shoppingcart.getScid());
            pstmt.setLong(2, shoppingcart.getUid());
            pstmt.setLong(3, shoppingcart.getSid());
            rows = pstmt.executeUpdate();
        } catch (SQLException e) {
            System.out.println("DAO 加入购物车失败：" + shoppingcart);
            e.printStackTrace();
        } finally {
            BaseDao.close(null, pstmt, conn);
        }
        return rows;
    }

    @Override
    public int delete(Shoppingcart shoppingcart) {
        int rows = 0;
        String sql = "DELETE FROM shoppingcart WHERE uid=? AND sid=?";
        Connection conn = BaseDao.getConnection();
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setLong(1, shoppingcart.getUid());
            pstmt.setLong(2, shoppingcart.getSid());
            rows = pstmt.executeUpdate();
        } catch (SQLException e) {
            System.out.println("DAO 删除购物车课程失败：" + shoppingcart);
            e.printStackTrace();
        } finally {
            BaseDao.close(null, pstmt, conn);
        }
        return rows;
    }

    @Override
    public int delete(long uid) {
        int rows = 0;
        String sql = "DELETE FROM shoppingcart WHERE uid=?";
        Connection conn = BaseDao.getConnection();
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setLong(1, uid);
            rows = pstmt.executeUpdate();
        } catch (SQLException e) {
            System.out.println("DAO 删除购物车课程失败：uid=" + uid);
            e.printStackTrace();
        } finally {
            BaseDao.close(null, pstmt, conn);
        }
        return rows;
    }

    @Override
    public int countById(long uid) {
        int count = 0;
        String sql = "select count(*) from shoppingcart WHERE uid=?";
        Connection conn = BaseDao.getConnection();
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setLong(1,uid);
            rs = pstmt.executeQuery();
            if (rs.next()) {
                count = rs.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            BaseDao.close(rs, pstmt, conn);
        }
        return count;
    }


}
